package cn.icanci.loopstack.ddk.web.controller.register;

import cn.icanci.loopstack.ddk.admin.biz.model.RegisterModel;
import cn.icanci.loopstack.ddk.admin.biz.service.RegisterService;
import cn.icanci.loopstack.ddk.common.model.PageList;
import cn.icanci.loopstack.ddk.common.result.R;
import cn.icanci.loopstack.ddk.common.socket.load.ResourceLoadRequestDTO;
import cn.icanci.loopstack.ddk.common.socket.load.ResourceLoadResponseDTO;
import cn.icanci.loopstack.ddk.common.socket.publish.RegisterDTO;
import cn.icanci.loopstack.ddk.web.form.PublishDictForm;
import cn.icanci.loopstack.ddk.web.form.RegisterQueryForm;
import cn.icanci.loopstack.ddk.web.mapping.RegisterWebMapping;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author icanci
 * @since 1.0 Created in 2023/01/07 15:05
 */
@RestController
@RequestMapping("/ddk/register")
public class RegisterController {
    @Resource
    private RegisterService    registerService;

    @Resource
    private RegisterWebMapping webMapping;

    @PostMapping("query")
    public R query(@RequestBody RegisterQueryForm form) {
        PageList<RegisterModel> queryPage = registerService.queryPage(form.getAppId(), form.getPaginator().getCurrentPage(), form.getPaginator().getPageSize());

        return R.builderOk().data("queryPage", queryPage).build();
    }

    @PostMapping("publishDict")
    public R publishDict(@RequestBody PublishDictForm publish) {
        registerService.publishDict(publish.getUuid());
        return R.builderOk().build();
    }

    /**
     * 注册
     * 
     * @param register register
     * @return 返回注册的结果
     */
    @PostMapping("doRegister")
    public R register(@RequestBody RegisterDTO register) {
        return R.builderOk().data("success", registerService.doRegister(register)).build();
    }

    /**
     * 加载
     * 
     * @param loadRequestDTO loadRequestDTO
     * @return 返回当前有权限的数据的结果
     */
    @PostMapping("load")
    public R load(@RequestBody ResourceLoadRequestDTO loadRequestDTO) {
        ResourceLoadResponseDTO responseDTO = registerService.loadByAppId(loadRequestDTO.getAppId());
        return R.builderOk().data("response", responseDTO).build();
    }
}
